package com.pocketsweet.ui;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.SaveCallback;
import com.easemob.chat.EMChatManager;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.pocketsweet.R;
import com.pocketsweet.C;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLCustomizationRecord;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.coutdownView;
import com.pocketsweet.ui.uilib.coutdownView.OnTimeCompleteListener;
import com.pocketsweet.utils.ToolKits;

@SuppressLint("ResourceAsColor")
@ContentView(R.layout.dialog_love_call)
public class DialogLoveCall extends BaseActivity implements OnClickListener {

	@ViewInject(R.id.btnOk)
	private static TextView btnOk;

	@ViewInject(R.id.imgCall)
	private static TextView imgCall;

	@ViewInject(R.id.linOk)
	private static LinearLayout linOk;

	@ViewInject(R.id.tvClose)
	private static ImageView tvClose;

	@ViewInject(R.id.tv_tips)
	private static TextView tv_tips;

	@ViewInject(R.id.countdownTimerLoveCall)
	private coutdownView countdownTimer;

	public MLUser mlUser;
	private int countdownTime = 180;// 倒计时时间
	MLCustomizationRecord mlCustomizationRecord;
	public static boolean isConnect = false;

	@SuppressLint("ResourceAsColor")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		mlUser = UserService.getCurrentUser();
		initView();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (isConnect) {
			initView();
		}
	}

	private void initView() {
		isConnect = false;
		if (mlUser.getBalance() >= 2) {
			btnOk.setText("确定");
			btnOk.setVisibility(View.VISIBLE);
			btnOk.setTextColor(getResources().getColor(R.color.white));
			linOk.setBackgroundResource(R.drawable.shape_btn_login);
			linOk.setClickable(true);
			imgCall.setVisibility(View.GONE);
			countdownTimer.setVisibility(View.GONE);
			linOk.setOnClickListener(this);
		} else {
			btnOk.setText("余额不足");
			btnOk.setTextColor(getResources().getColor(
					R.color.main_text_color_pink));
			linOk.setBackgroundResource(R.drawable.shape_btn_sure_grey);
			linOk.setClickable(false);
			imgCall.setVisibility(View.GONE);
			countdownTimer.setVisibility(View.GONE);

		}
		tvClose.setOnClickListener(this);
		tv_tips.setText(getResources().getString(R.string.loveCall_tips));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.linOk:
			linOk.setClickable(false);
			mlCustomizationRecord = new MLCustomizationRecord();
			mlCustomizationRecord.saveInBackground(new SaveCallback() {// 保存一条纪录
						@Override
						public void done(AVException e) {
							if (e == null) {
								// 调用云代码发一条透传给恋爱体验师
								Map<String, Object> params = new HashMap<String, Object>();

								if (mlUser.getGender() == C.USER_GENDER_FEMALE)
									params.put("type", C.USER_GENDER_MALE);
								else
									params.put("type", C.USER_GENDER_FEMALE);

								AVCloud.callFunctionInBackground(
										"sendCustomizationMsg", params, null);

								//
								imgCall.setVisibility(View.VISIBLE);
								btnOk.setTextColor(getResources().getColor(
										R.color.main_text_color_pink));
								linOk.setBackgroundResource(R.drawable.shape_btn_sure_grey);
								tv_tips.setText(getResources().getString(
										R.string.loveCalling_tips));
								countdownTimer.initTime(countdownTime);
								countdownTimer.setVisibility(View.VISIBLE);
								btnOk.setVisibility(View.GONE);
								// 启动倒计时
								countdownTimer.start();
								countdownTimer
										.setOnTimeCompleteListener(new OnTimeCompleteListener() {

											@Override
											public void onTimeComplete() {
												mlCustomizationRecord
														.deleteInBackground(new DeleteCallback() {

															@Override
															public void done(
																	AVException arg0) {
																initView();
															}
														});
											}
										});
							} else {
								ToolKits.toast(DialogLoveCall.this, "请检查你的网络");
							}
						}
					});
			break;
		case R.id.tvClose:
			DialogLoveCall.this.finish();
			break;
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mlCustomizationRecord != null) {
			mlCustomizationRecord.deleteInBackground();
		}
		if (countdownTimer.isActivated()) {
			countdownTimer.stop();
		}
	}

}
